package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lin.entity.GroupInformation;
import com.lin.entity.ProjectDetail;
import com.lin.entity.ProjectOutside;
import com.lin.entity.TalentJoin;
import com.lin.mapper.ProjectOutsideMapper;
import com.lin.mapper.TalentJoinMapper;
import com.lin.security.util.SecurityUtil;
import com.lin.service.GroupInformationService;
import com.lin.service.ProjectDetailService;
import com.lin.service.ProjectOutsideService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.service.TalentJoinService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林伟鸿
 * @since 2022-04-16
 */
@Service
public class ProjectOutsideServiceImpl extends ServiceImpl<ProjectOutsideMapper, ProjectOutside> implements ProjectOutsideService {


    @Resource
    ProjectDetailService projectDetailService;

    @Resource
    GroupInformationService groupInformationService;

    @Resource
    TalentJoinMapper TalentJoinMapper;

    @Override
    public void publishProject(ProjectOutside projectOutside) {
        int currentUserUid = SecurityUtil.getCurrentUserUid();
        if (this.getOne(new QueryWrapper<ProjectOutside>()
                .eq("project_name", projectOutside.getProjectName()).eq("master_id", currentUserUid)) != null) {
            throw new RuntimeException("您已经发布过同名的项目，无法重复发布");
        }
        projectOutside.setMasterId(currentUserUid);
        GroupInformation groupInformation = new GroupInformation();
        groupInformation.setMasterId(currentUserUid);
        groupInformationService.save(groupInformation);
        this.save(projectOutside);
        ProjectDetail projectDetail = new ProjectDetail();
        BeanUtils.copyProperties(projectOutside, projectDetail);
        projectDetail.setGroupId(groupInformation.getGroupId());
        projectDetailService.save(projectDetail);
    }

    @Override
    public List<TalentJoin> getJoinsList() {
        int currentUserUid = SecurityUtil.getCurrentUserUid();
        //拿到当前用户所有的项目
        List<ProjectOutside> projectOutsideList = this.list(new QueryWrapper<ProjectOutside>().eq("master_id", currentUserUid));
        List<TalentJoin> talentJoinList = new ArrayList<>();
        //通过projectId，去talent_join表当中查询
        for (ProjectOutside projectOutside : projectOutsideList) {
            TalentJoin talentJoin = TalentJoinMapper.selectOne(new QueryWrapper<TalentJoin>()
                    .eq("project_id", projectOutside.getProjectId()));
            if (talentJoin != null) {

                talentJoinList.add(talentJoin);
            }
        }
        return talentJoinList;
    }
}
